/*******************************************************************************
 * Copyright (c) 2010 Marcelo Cataldo, Sangeeth Nambiar
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * DepsGraphNodeSet.java
 * 
 * Implements an abstraction layer for a dependency graph nodeset, which is
 * a collection of dependency graph nodes.
 * 
 * Contributors:
 *    Marcelo Cataldo  - initial implementation
 *    Sangeeth Nambiar - initial implementation 
 *******************************************************************************/
package edu.cmu.isr.dinvenio.model;

import java.util.Enumeration;
import java.util.Hashtable;

import edu.cmu.isr.dinvenio.exceptions.InvalidObjectTypeException;

public class DepsGraphNodeSet {
	
	public static final String ORG_NODESET = "organization";
	public static final String TECH_NODESET = "technical";
	
	private String id;       // Name of the NodeSet
	private String type;     // type of nodeset
	public  Hashtable<String,DepsGraphNode> nodes;	// contains the actual in-core representation of a node

	/*
	 * Constructors
	 */
	public DepsGraphNodeSet(String nsid, String nstype) throws InvalidObjectTypeException {
		this.id   = nsid;
		this.type = nstype;
		if (!((this.type.equals(ORG_NODESET) ) ||  (this.type.equals(TECH_NODESET)))) {
			throw new InvalidObjectTypeException();
		}
		nodes = new Hashtable<String,DepsGraphNode>();
	}

	/*
	 * ACCESSOR methods
	 */
	public String getId() 
	{
		return this.id;
	}
	
	public String getType() 
	{
		return this.type;
	}
	
	/*
	 * Methods for ADDING/REMOVING elements
	 */
	public void addNode(DepsGraphNode n) 
	{
		//nodes.put(n.getId(), n);
		nodes.put(DepsGraphNode.generateKey(this.id, n.getId()), n);
	}
	
	public void addNodeProperty(String nid, String pn, String pt, String pv) 
	{
		DepsGraphNode n = nodes.get(DepsGraphNode.generateKey(this.id, nid));
		if (n == null) { throw new NullPointerException(); }
		n.addProperty(pn, pt, pv);
	}
	
//	public void removeNodeByKey(String nid) 
//	{
//		DepsGraphNode n = nodes.get(nid);
//		if (n == null) { throw new NullPointerException(); }
//		removeNodeByObject(n);
//	}
	
//	public void removeNodeByObject(DepsGraphNode n) 
//	{
//		//nodes.remove(n.getId());
//		nodes.remove(DepsGraphNode.generateKey(this.id, n.getId()));
//	}

	/*
	 * Methods for SEARCHING nodes
	 */
	public boolean isTechnicalNodeSet()
	{
		return (this.type == TECH_NODESET);
	}
	
	public boolean isOrganizationalNodeSet()
	{
		return (this.type == ORG_NODESET);
	}
	
	public boolean hasNodeByName(String name) 
	{
		return nodes.containsKey(DepsGraphNode.generateKey(this.id, name));
	}
	
//	public boolean hasNodeByKey(String nkey) 
//	{
//		return nodes.containsKey(nkey);
//	}
	
	public DepsGraphNode getNodeByName(String nid)
	{
		return nodes.get(DepsGraphNode.generateKey(this.id, nid));
	}
	
//	public DepsGraphNode getNodeByKey(String nkey)
//	{
//		return nodes.get(nkey);
//	}
	
	public Enumeration<DepsGraphNode> getNodes() 
	{
		return nodes.elements();
	}
	
//	public Set<String> getNodeNames() 
//	{
//		return nodes.keySet();
//	}
}
